import { regionMap } from '@/utils/regionMap'

// scenicList: [{ regionId, scenicAreaName, ... }]
export function getProvinceData(scenicList) {
  if (!Array.isArray(scenicList)) return [];
  const provinceData = {};
  scenicList.forEach(spot => {
    if (!spot || spot.regionId == null) return;
    // 强制regionId为字符串，防止和regionMap的key类型不一致
    const region = regionMap[String(spot.regionId)];

    // 打印调试信息
    console.log(
      'regionId:', spot.regionId,
      'region:', region,
      'province:', region && region.province
    );

    if (!region || !region.province) return;

    // 省份名去除后缀，与geoJson保持一致
    let province = region.province.replace(/(省|市|自治区|特别行政区|壮族自治区|回族自治区|维吾尔自治区|蒙古自治区)$/, '');
    if (!provinceData[province]) provinceData[province] = [];
    // scenicAreaName不存在时不push
    if (spot.scenicAreaName) provinceData[province].push(spot.scenicAreaName);
  });
  return Object.entries(provinceData).map(([province, spots]) => ({
    name: province,
    value: spots.length,
    spots
  }));
}